package org.bonorsoft.modules.education.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bonorsoft.modules.education.entity.Addressee;
import org.bonorsoft.modules.education.vo.NoticeVo;

import java.util.Date;
import java.util.List;

/**
 * @Description: 收件人表
 * @Author: WilliamYe
 * @Date: 2021-06-22
 * @Version: V1.0
 */
public interface AddresseeMapper extends BaseMapper<Addressee> {

    /**
     * 添加收件人
     *
     * @param uuid
     * @param noticeId
     * @param addresseeName
     * @return
     */
    @Insert("insert into sys_addressee(addressee_id, notice_id, addressee_name) " +
            "values (#{uuid}, #{noticeId}, #{addresseeName})")
    boolean add(String uuid, String noticeId, String addresseeName);

    @Select("select addressee_id, notice_id, addressee_name from sys_addressee " +
            "where notice_id = #{noticeId} ")
    List<Addressee> getByNoticeId(String noticeId);

    @Select("select addressee_id, notice_id, addressee_name, read_time from sys_addressee " +
            "where notice_id = #{noticeId} and addressee_read = '1' ")
    List<Addressee> getYesByNotice(String noticeId);

    @Select("select addressee_id, notice_id, addressee_name from sys_addressee " +
            "where notice_id = #{noticeId} and addressee_read = '0' ")
    List<Addressee> getNoByNotice(String noticeId);

    @Select("select addressee_id, notice_id, addressee_name, sign_up, participants_name from sys_addressee " +
            "where notice_id = #{noticeId} and addressee_read = '1' and sign_up = '1' ")
    List<Addressee> getSignByNoticeId(String noticeId);

    @Select("select n.notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag, " +
            "addressee_id, addressee_name, addressee_read, read_time, sign_up, participants_name " +
            "from sys_notice n ,sys_addressee a " +
            "${ew.customSqlSegment} and n.notice_id = a.notice_id " +
            "order by addressee_read asc , notice_sending_time desc ")
    IPage<NoticeVo> paging(Page<NoticeVo> page, @Param(Constants.WRAPPER) QueryWrapper<NoticeVo> queryWrapper);

    @Delete("delete from sys_addressee " +
            "where notice_id = #{noticeId} ")
    void removeByNoticeId(String id);

    @Select("select addressee_id, notice_id, addressee_name from sys_addressee " +
            "where notice_id = #{noticeId} and addressee_name = #{addresseeName} ")
    Addressee getByName(String noticeId, String addresseeName);

    @Select("select n.notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag, " +
            "addressee_id, addressee_name, addressee_read, read_time, sign_up, participants_name " +
            "from sys_notice n ,sys_addressee a " +
            "where n.notice_id = a.notice_id and addressee_name = #{mobile} " +
            "order by addressee_read asc , notice_sending_time desc ")
    List<NoticeVo> mobileList(String mobile);

    @Select("select n.notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag, " +
            "addressee_id, addressee_name, addressee_read, read_time, sign_up, participants_name " +
            "from sys_notice n ,sys_addressee a  ${ew.customSqlSegment} " +
            "and n.notice_id = a.notice_id and notice_theme like '%'||#{keyWord}||'%' " +
            "and notice_sending_time between #{startTime} and #{endTime} " +
            "order by addressee_read asc , notice_sending_time desc ")
    List<NoticeVo> searchList( @Param(Constants.WRAPPER) QueryWrapper<NoticeVo> queryWrapper, String keyWord, String startTime, String endTime);
}
